package ${filePackageName};

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import springfox.documentation.annotations.ApiIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import ${commonBeanPackage}.EntityData;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


/**
 * 基础工具控制器
 *
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
@ApiIgnore
@Controller
public class BaseController {

    private Logger logger = LoggerFactory.getLogger(BaseController.class);


    @Resource
    private HttpServletResponse response;

    @Resource
    private HttpServletRequest request;


    /**
     * 获取EntityData
     * @param clazz
     * @param entityName
     * @param primaryKey
     * @return
     */
    protected EntityData getEntityData(Class clazz, String entityName, String primaryKey){
        EntityData entityData = new EntityData();
        entityData.setEntityName(entityName);
        entityData.setPrimaryKey(primaryKey);
        entityData.setAccessBaseUrl("${accessBaseUrl}");

        ApiModel apiModel = (ApiModel) clazz.getAnnotation(ApiModel.class);
        if(apiModel!=null){
            String entityComment = apiModel.value();
            entityData.setEntityComment(entityComment);
        }

        Map<String,Object> entityAttrs = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
            if(property!=null){
                String comment = property.value();
                String name = field.getName();
                entityAttrs.put(name,comment);
            }
        }
        entityData.setEntityAttrs(entityAttrs);
        return entityData;
    }

    /**
     * 获取查询条件
     * @param clazz
     * @return
     */
    protected Map<String,Object> getSearchEntityAttrs(Class clazz){
        Map<String,Object> entityAttrs = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
            if(property!=null){
                String comment = property.value();
                String name = field.getName();
                entityAttrs.put(name,comment);
            }
        }
        return entityAttrs;
    }

}
